package com.surveyingandmapping.ionospheretec.service.impl;

import com.surveyingandmapping.ionospheretec.model.ObservationData;
import com.surveyingandmapping.ionospheretec.model.TECResult;
import com.surveyingandmapping.ionospheretec.service.FileService;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * Created by Carly rea jepsen on 2016/3/22.
 */
@Service
public class FileServiceImpl implements FileService {

    public final static List<ObservationDataDto> observationDtoList = new ArrayList<ObservationDataDto>();

    public final static List<ObservationData> observationDataList = new ArrayList<ObservationData>();

    /**
     * 计算VTEC
     * @return
     */
    public List<TECResult> getTecRsult()
    {

        return null;
    }

    /**
     * 读取单个测量文件
     * @param file
     * @return
     */
    public void getObservationData(File file) throws IOException,ParseException
    {

        Assert.notNull(file,"上传失败文件为空");
        String s = null;
        s = FileUtils.readFileToString(file,"UTF-8");
        String[] str = StringUtils.split(s,Constant.SPACE_SYMBOL);
        Stack<String> type = DataProcessing.sortObservationData(str);
        Assert.notNull(type,"文件内容有误");
        List<ObservationData> allObservationData = DataProcessing.addAllObservation(str,type);
        this.observationDataList.addAll(allObservationData);
        for (ObservationData observationData : allObservationData){
            ObservationDataDto observationDataDto = DataProcessing.transformObservationData(observationData);
            observationDtoList.add(observationDataDto);
        }

    }

    /**
     * 将数据保存为Excele
     * @param path
     * @throws IOException
     * @throws WriteException
     * @throws ParseException
     */
    public void SaveData(String path) throws IOException,WriteException,ParseException
    {
        WritableWorkbook book = Workbook.createWorkbook(new File(path));
        WritableSheet sheet = book.createSheet("GPS观测数据",0);
        DataProcessing.addExcelHead(sheet);
        for (int i=0;i<this.observationDataList.size();i++){
            ObservationData observationData = this.observationDataList.get(i);
            DataProcessing.addExcelValue(sheet,i,observationData);
        }
        book.write();
        book.close();

    }

    /**
     * 获取同一历元数据按卫星排序
     * @param epoch
     * @return
     */
    public List<ObservationData> sortObservationDataByEpoch(String epoch)
    {

        return null;
    }

    /**
     * 获取同一颗卫星的所有数据按时间排序
     * @param satelliteNo
     * @return
     */
    public List<ObservationData> sortObservationDataBySatelliteNo(int satelliteNo)
    {
        List<ObservationData> observationDatas = new ArrayList<ObservationData>();
        Iterator<ObservationData> iterator = observationDataList.iterator();
        while(iterator.hasNext()){
            ObservationData observationData = iterator.next();
            if (observationData.getSatelliteNo()==satelliteNo){
                observationDatas.add(observationData);
            }
        }

        return null;
    }

}
